//
// Created by A4-28 on 2020/10/12.
//

#include <iostream>
#include "perspectiveCamera.h"

namespace WF {

    PerspectiveCamera::PerspectiveCamera() {}

    void PerspectiveCamera::ReCalculate() {
        Update();

        projectVewMatrix = projectMatrix * viewMatrix;
    }

    void PerspectiveCamera::Update() {
        Forward.x = glm::cos(Pitch) * glm::sin(Yaw);
        Forward.y = glm::sin(Pitch);
        Forward.z = glm::cos(Pitch) * glm::cos(Yaw);
        Right = glm::normalize(glm::cross(Forward, WorldUp));
        Up = glm::normalize(glm::cross(Forward, Right));
        Forward = glm::normalize(Forward);
    }

    glm::mat4 PerspectiveCamera::GetViewMatrix() {
        viewMatrix = glm::lookAt(Position, Position + Forward, WorldUp);
        return viewMatrix;
    }

    glm::mat4 PerspectiveCamera::GetProjectMatrix() {
        projectMatrix = glm::perspective(glm::radians(FOV), (float) width / (float) height, zNear, zFar);
        return projectMatrix;
    }
    //modelMatrix == translateMatrix * scaleMatrix * rotateMatrix(使用四元数计算)
    glm::mat4 PerspectiveCamera::GetMVPMatrix() {
        glm::mat4 modelMatrix = glm::translate(glm::mat4(1), Position);
        return projectMatrix * viewMatrix * modelMatrix;
    }
}